package org.loofer.ext.component.info;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build.VERSION;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import org.loofer.ext.Ext;
import org.loofer.ext.component.logger.Logger;
import io.rong.imlib.statistics.UserData;

abstract class NetworkObserver extends BroadcastReceiver {
    private static final String TAG = "NetworkObserver";
    private volatile int cellLevel;
    private final boolean cellListenEnabled;
    private PhoneStateListener signalListener;

    public abstract void onNetworkChanged();

    NetworkObserver() {
        this.cellListenEnabled = VERSION.SDK_INT >= 7;
        this.cellLevel = -1;
    }

    public void startListen() {
        try {
            Ext.getContext().registerReceiver(this, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
        } catch (Exception e) {
            Logger.w(TAG, "startListen failed");
        }
    }

    public void stopListen() {
        Ext.getContext().unregisterReceiver(this);
    }

    public int getCellLevel() {
        return this.cellLevel;
    }

    public void onReceive(Context context, Intent intent) {
        if (this.cellListenEnabled && this.signalListener == null) {
            synchronized (this) {
                if (this.signalListener == null) {
                    initSignalListen();
                }
            }
        }
        if ("android.net.conn.CONNECTIVITY_CHANGE".equals(intent.getAction())) {
            onNetworkChanged();
        }
    }

    @SuppressLint({"InlinedApi"})
    private void initSignalListen() {
        if (this.cellListenEnabled) {
            this.signalListener = new PhoneStateListener() {
                public void onSignalStrengthsChanged(SignalStrength signalStrength) {
                    NetworkObserver.this.cellLevel = NetworkObserver.this.getCellLevel(signalStrength);
                    super.onSignalStrengthsChanged(signalStrength);
                }
            };
            TelephonyManager telephonyManager = (TelephonyManager) Ext.getContext().getSystemService(UserData.PHONE_KEY);
            if (telephonyManager != null) {
                telephonyManager.listen(this.signalListener, 256);
            } else {
                this.signalListener = null;
            }
        }
    }

    private int getCellLevel(SignalStrength signalStrength) {
        if (signalStrength == null) {
            return -1;
        }
        int level;
        if (signalStrength.isGsm()) {
            level = getGsmLevel(signalStrength);
        } else {
            int cdmaLevel = getCdmaLevel(signalStrength);
            int evdoLevel = getEvdoLevel(signalStrength);
            if (evdoLevel == 0) {
                level = cdmaLevel;
            } else if (cdmaLevel == 0) {
                level = evdoLevel;
            } else {
                level = evdoLevel > cdmaLevel ? cdmaLevel : evdoLevel;
            }
        }
        return level;
    }

    private int getGsmLevel(SignalStrength signalStrength) {
        int asu = signalStrength.getGsmSignalStrength();
        if (asu <= 2 || asu == 99) {
            return 0;
        }
        if (asu >= 12) {
            return 4;
        }
        if (asu >= 8) {
            return 3;
        }
        if (asu < 5) {
            return 1;
        }
        return 2;
    }

    private int getCdmaLevel(SignalStrength signalStrength) {
        int levelDbm;
        int levelEcio;
        int cdmaDbm = signalStrength.getCdmaDbm();
        int cdmaEcio = signalStrength.getCdmaEcio();
        if (cdmaDbm >= -75) {
            levelDbm = 4;
        } else if (cdmaDbm >= -85) {
            levelDbm = 3;
        } else if (cdmaDbm >= -95) {
            levelDbm = 2;
        } else if (cdmaDbm >= -100) {
            levelDbm = 1;
        } else {
            levelDbm = 0;
        }
        if (cdmaEcio >= -90) {
            levelEcio = 4;
        } else if (cdmaEcio >= -110) {
            levelEcio = 3;
        } else if (cdmaEcio >= -130) {
            levelEcio = 2;
        } else if (cdmaEcio >= -150) {
            levelEcio = 1;
        } else {
            levelEcio = 0;
        }
        if (levelDbm < levelEcio) {
            return levelDbm;
        }
        return levelEcio;
    }

    private int getEvdoLevel(SignalStrength signalStrength) {
        int levelEvdoDbm;
        int levelEvdoSnr;
        int evdoDbm = signalStrength.getEvdoDbm();
        int evdoSnr = signalStrength.getEvdoSnr();
        if (evdoDbm >= -65) {
            levelEvdoDbm = 4;
        } else if (evdoDbm >= -75) {
            levelEvdoDbm = 3;
        } else if (evdoDbm >= -90) {
            levelEvdoDbm = 2;
        } else if (evdoDbm >= -105) {
            levelEvdoDbm = 1;
        } else {
            levelEvdoDbm = 0;
        }
        if (evdoSnr >= 7) {
            levelEvdoSnr = 4;
        } else if (evdoSnr >= 5) {
            levelEvdoSnr = 3;
        } else if (evdoSnr >= 3) {
            levelEvdoSnr = 2;
        } else if (evdoSnr >= 1) {
            levelEvdoSnr = 1;
        } else {
            levelEvdoSnr = 0;
        }
        if (levelEvdoDbm < levelEvdoSnr) {
            return levelEvdoDbm;
        }
        return levelEvdoSnr;
    }
}
